package com.atguigu.gmall.product.config;

import com.atguigu.gmall.rabbit.constant.RabbitConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //创建交换机
    @Bean(RabbitConst.EXCHANGE_DIRECT_GOODS)
    public Exchange createExchange(){
        return ExchangeBuilder.directExchange(RabbitConst.EXCHANGE_DIRECT_GOODS).build();
    }

    //创建上架队列
    @Bean(RabbitConst.QUEUE_GOODS_UPPER)
    public Queue createUpperQueue(){
        return QueueBuilder.durable(RabbitConst.QUEUE_GOODS_UPPER).build();
    }

    //创建下架队列
    @Bean(RabbitConst.QUEUE_GOODS_LOWER)
    public Queue createLowerQueue(){
        return QueueBuilder.durable(RabbitConst.QUEUE_GOODS_LOWER).build();
    }
    //绑定上架队列
    @Bean
    public Binding createUpperBinding(@Qualifier(RabbitConst.QUEUE_GOODS_UPPER) Queue queue,
                                 @Qualifier(RabbitConst.EXCHANGE_DIRECT_GOODS) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RabbitConst.ROUTING_GOODS_UPPER).noargs();
    }

    //绑定下架队列
    @Bean
    public Binding createLowerBinding(@Qualifier(RabbitConst.QUEUE_GOODS_LOWER) Queue queue,
                                      @Qualifier(RabbitConst.EXCHANGE_DIRECT_GOODS) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitConst.ROUTING_GOODS_LOWER).noargs();
    }
}
